package com.cloudkinto.controller.order.allocate;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.order.allocate.AllocatePlanDetailBo;
import com.cloudkinto.bo.order.allocate.AllocatePlanQueryBo;
import com.cloudkinto.bo.order.allocate.AllocatePlanUpdateBo;
import com.cloudkinto.bo.stock.ProductStockBySkuQueryBo;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.order.allocate.AllocatePlanService;
import com.cloudkinto.service.order.outbound.OutboundImportExportService;
import com.cloudkinto.vo.order.allocate.AllocatePlanBatchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 调拨发货计划 前端控制器
 * </p>
 *
 * @author long
 * @since 2025-03-11
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "Omp调拨发货计划模块")
@RequestMapping("/api/admin/omp/allocatePlan")
public class AllocatePlanOmpController extends BaseController {

    private final AllocatePlanService allocatePlanService;
    private final OutboundImportExportService outboundImportExportService;

    @UserSessionRequired
    @PutMapping("/update")
    @ApiOperation(value = "调拨发货计划修改", notes = "调拨发货计划修改")
    public SingleResult update(@RequestBody @Validated AllocatePlanUpdateBo req) {
        allocatePlanService.update(req, getCurrentUserId(), getUserName());
        return SingleResult.success("修改成功");
    }

    @UserSessionRequired
    @PostMapping("/detail")
    @ApiOperation(value = "调拨发货计划根据id查看详情", notes = "调拨发货计划根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = AllocatePlanBatchVo.class))
    public SingleResult<AllocatePlanBatchVo> detail(@RequestBody @Validated AllocatePlanDetailBo detail) {
        ProductStockBySkuQueryBo productStockBySkuQueryBo = new ProductStockBySkuQueryBo();
        productStockBySkuQueryBo.setStockAttribute(0);
        productStockBySkuQueryBo.setSource("omp");
        productStockBySkuQueryBo.setTenantId(getTenantId());
        productStockBySkuQueryBo.setUserId(getCurrentUserId());
        return SingleResult.success("查询成功", allocatePlanService.detail(detail, productStockBySkuQueryBo));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "调拨发货计划查询初始化", notes = "调拨发货计划查询初始化")
    public SingleResult<Map<String, Object>> pageInit() {
        return allocatePlanService.pageInit(getCurrentUserId(), getTenantId(), getCompanyId(), "omp");
    }

    @UserSessionRequired
    @PostMapping("/listPage")
    @ApiOperation(value = "调拨批量发货计划查询（分页）", notes = "调拨批量发货计划查询（分页）")
    @ApiResponses(@ApiResponse(code = 200, message = "查询成功", response = AllocatePlanBatchVo.class))
    public PageResult<AllocatePlanBatchVo> listPage(@RequestBody @Validated AllocatePlanQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("omp");
        return allocatePlanService.getListPage(bo);
    }

    @UserSessionRequired
    @PostMapping("/listPageStatusSta")
    @ApiOperation(value = "调拨批量发货计划查询分页统计", notes = "调拨批量发货计划查询分页统计")
    public SingleResult<List<Map<String, Object>>> listPageStatusSta(@RequestBody @Validated AllocatePlanQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("omp");
        return allocatePlanService.getListPageStatusSta(bo);
    }

    @UserSessionRequired
    @PostMapping("/allocateExportBy3rdWh")
    @ApiOperation(value = "调拨发货计划导出三方仓", notes = "调拨发货计划导出三方仓")
    public void allocateExportBy3rdWh(@RequestBody @Validated AllocatePlanQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("omp");
        outboundImportExportService.allocateExportBy3rdWh(bo, response);
    }

    @UserSessionRequired
    @PostMapping("/allocateExportByFba")
    @ApiOperation(value = "调拨发货计划导出fba", notes = "调拨发货计划导出fba")
    public void allocateExportByFba(@RequestBody @Validated AllocatePlanQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("omp");
        outboundImportExportService.allocateExportByFba(bo, response);
    }
}